package com.xr.pfmWisdom;

import java.util.List;

import com.xr.common.CommUtil;
import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;

/**
 * jxDeptRoleSetViewModel
 */
public class jxDeptRoleSetViewModel extends DefaultViewModel {
  protected void doUpdateData(ParameterSet parameters, ParameterSet outParameters)
      throws Exception {
    // Add your code here
    super.doUpdateData(parameters, outParameters);
  } 
  public void deptRoleSetSave(ParameterSet parameters, ParameterSet outParameters)
  throws Exception {
Dataset comDs= getDataset("datasetCom");
Dataset deptDs= getDataset("datasetDept");
Dataset empDs= getDataset("datasetEmp");
Dataset roleDs= getDataset("datasetPosition");
Dataset dsC= getDataset("datasetCheck");

String filesId=deptDs.parameters().getString("PJX_CHECK_ID");
DBStatement dbs = new DBStatement();
int fileType=parameters.getInt("TYPE");
comDs.moveFirst();
while (!comDs.isLast()) {
  if(comDs.getBoolean("select")){ 
	 // System.out.println("======"+deptDs.getString("SUPER_DEPT_ID"));
	  dbs.setSql("select * from pjx_dept_role where PJX_CHECK_ID="+filesId
	  		+" and DEPT_ROLE_ID="+comDs.getString("COMPANY_ID")+" and TYPE=4");
	  List list=dbs.queryForList();
	  
	  if(list.size()==0){
	  dbs.setSql("insert into pjx_dept_role" +
	  		"(PJX_DEPT_ROLE_ID,PJX_CHECK_ID,DEPT_ROLE_ID,TYPE)" +
	  		"values(general.nextval,"+filesId+","+comDs.getString("COMPANY_ID")+",4)");
	  dbs.execute();
	  }
  }else{
	  dbs.setSql("delete from pjx_dept_role " +
	  		"where PJX_CHECK_ID="+filesId
	  		+" and DEPT_ROLE_ID="+comDs.getString("COMPANY_ID")+" and TYPE=4");
  	  dbs.execute();
  }
  comDs.moveNext();
}

deptDs.moveFirst();
while (!deptDs.isLast()) {
  if(deptDs.getBoolean("select")){ 
	 // System.out.println("======"+deptDs.getString("SUPER_DEPT_ID"));
	  dbs.setSql("select * from pjx_dept_role where PJX_CHECK_ID="+filesId
	  		+" and DEPT_ROLE_ID="+deptDs.getString("DEPT_ID")+" and TYPE=1");
	  List list=dbs.queryForList();
	  
	  if(list.size()==0){
	  dbs.setSql("insert into pjx_dept_role" +
	  		"(PJX_DEPT_ROLE_ID,PJX_CHECK_ID,DEPT_ROLE_ID,TYPE)" +
	  		"values(general.nextval,"+filesId+","+deptDs.getString("DEPT_ID")+",1)");
	  dbs.execute();
	  }
  }else{
	  dbs.setSql("delete from pjx_dept_role " +
	  		"where PJX_CHECK_ID="+filesId
	  		+" and DEPT_ROLE_ID="+deptDs.getString("DEPT_ID")+" and TYPE=1");
  	  dbs.execute();
  }
  deptDs.moveNext();
}

		empDs.moveFirst();
		while (!empDs.isLast()) {
			if (empDs.getBoolean("select")) {
				dbs.setSql("select * from pjx_dept_role where PJX_CHECK_ID="
								+ filesId
								+ " and DEPT_ROLE_ID="
								+ empDs.getString("EMPLOYEE_ID")
								+ " and TYPE=3");
				List list = dbs.queryForList();

				if (list.size() == 0) {
					dbs.setSql("insert into pjx_dept_role"
									+ "(PJX_DEPT_ROLE_ID,PJX_CHECK_ID,DEPT_ROLE_ID,TYPE)"
									+ "values(general.nextval," + filesId
									+ "," + empDs.getString("EMPLOYEE_ID")
									+ ",3)");
					dbs.execute();
				}
			} else {
				dbs.setSql("delete from pjx_dept_role "
						+ "where PJX_CHECK_ID=" + filesId
						+ " and DEPT_ROLE_ID="
						+ empDs.getString("EMPLOYEE_ID") + " and TYPE=3");
				dbs.execute();
			}
			empDs.moveNext();
		}
	  
	
roleDs.moveFirst();
while (!roleDs.isLast()) {
    //System.out.println(deptDs.getString("PROVIDER_ID")+"=="+deptDs.getString("PROVIDER_NAME")+"==="+deptDs.getBoolean("select"));
    if(roleDs.getBoolean("ISCHECKED")){
  	  dbs.setSql("select * from pjx_dept_role where PJX_CHECK_ID="+filesId
  	  		+" and DEPT_ROLE_ID="+roleDs.getString("POSITION_ID")+" and TYPE=2");
  	  List list=dbs.queryForList();
  	  
  	  if(list.size()==0){
  	  dbs.setSql("insert into pjx_dept_role" +
  	  		"(PJX_DEPT_ROLE_ID,PJX_CHECK_ID,DEPT_ROLE_ID,TYPE)" +
  	  		"values(general.nextval,"+filesId+","+roleDs.getString("POSITION_ID")+",2)");
  	  dbs.execute();
  	  }
    }else{
  	  dbs.setSql("delete from pjx_dept_role " +
  	  		"where PJX_CHECK_ID="+filesId
  	  		+" and DEPT_ROLE_ID="+roleDs.getString("POSITION_ID")+" and TYPE=2");
    	  dbs.execute();
    }
    roleDs.moveNext();        
  }
 CommUtil.dbsUpdateData(dbs, dsC, "pjx_check", "PJX_CHECK_ID");
 
/*    if (fileType == 2) { // if file

	empDs.moveFirst();
	  String supperFilesId=parameters.getString("SUPPER_FILE_ID");
	while (!empDs.isLast()) {
		if (empDs.getBoolean("select")) {
			
			dbs.setSql("select * from pjx_dept_role where PJX_CHECK_ID="
							+ supperFilesId
							+ " and DEPT_ROLE_ID="
							+ empDs.getString("EMPLOYEE_ID")
							+ " and TYPE=3");
			List list = dbs.queryForList();

			if (list.size() == 0) {
				dbs.setSql("insert into pjx_dept_role"
								+ "(PJX_DEPT_ROLE_ID,PJX_CHECK_ID,DEPT_ROLE_ID,TYPE)"
								+ "values(general.nextval," + supperFilesId
								+ "," + empDs.getString("EMPLOYEE_ID")
								+ ",3)");
				dbs.execute();
			}
			
		} else {
			
			dbs.setSql("delete from pjx_dept_role "
					+ "where PJX_CHECK_ID=" + supperFilesId
					+ " and TYPE=3 and not exists(select 'X' from rt_pvd_dept_employee where employee_id=DEPT_ROLE_ID ) and DEPT_ROLE_ID="
					+ empDs.getString("EMPLOYEE_ID") + " ");
			dbs.execute();
		}
		empDs.moveNext();
	}

}
*/
dbs.close();
}

}
